import {map} from "core-js/internals/array-iteration";
import {getUserInfo} from "@/api/user";

const state = () => ({
    userInfo:{
        userId:'',
        avatar: '',
        username: '',
        nickname: '',
        backgroundImage:'',
        phone:'',
        email:''
    },
})

const getters = {

}
const actions = {
    async getUserInfo(context,vue) {
        //查看localstorage
        context.state.userInfo = JSON.parse(localStorage.getItem("access-admin"))
        console.log(context.state.userInfo)
        if(context.state.userInfo===null||context.state.userInfo.userId===''){
            await getUserInfo().then(res => {
                if(res.code === 200){
                    context.commit('setUserInfo', res.data['userInfo'])
                }else{
                    localStorage.removeItem("access-admin")
                    localStorage.removeItem("token")
                    vue.$router.push("/login")
                }
            })
        }
        return context.state.userInfo
    },
}

const mutations = {

    setUserInfo(state,data) {
        state.userInfo = data
        localStorage.setItem("access-admin",JSON.stringify(data))
    }
}

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}
